package com.heima.feigns.fallback;

import com.heima.feigns.ArticleFeign;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.search.vos.SearchArticleVo;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class ArticleFallBack implements FallbackFactory<ArticleFeign> {
    @Override
    public ArticleFeign create(Throwable throwable) {
        throwable.printStackTrace();//打印远程调用的异常
        return new ArticleFeign() {
            @Override
            public ResponseResult findAuthorByUserId(Integer id) {
                log.error("ArticleFeign findAuthorByUserId 远程调用出错，参数：{} 错误原因：{}", id, throwable.getMessage());
                return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
            }

            @Override
            public ResponseResult addAuthor(ApAuthor apAuthor) {
                log.error("ArticleFeign addAuthor 远程调用出错，参数：{} 错误原因：{}", apAuthor, throwable.getMessage());
                return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
            }

            @Override
            public ResponseResult addArticle(ArticleDto articleDto) {
                log.error("ArticleFeign addArticle 远程调用出错，参数：{} 错误原因：{}", articleDto, throwable.getMessage());
                return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
            }

            @Override
            public ResponseResult<SearchArticleVo> findArticle(Long id) {
                log.error("ArticleFeign findArticle 远程调用出错，参数：{} 错误原因：{}", id, throwable.getMessage());
                return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
            }
        };
    }
}
